import jieba


from .browser_utils import *
from .image_utils import *


def compare(str1, str2):
    if len(str1) > len(str2):
        tmp = str1
        str1 = str2
        str2 = tmp
    list1 = list(jieba.cut(str1))
    list2 = list(jieba.cut(str2))

    count = 0

    for item1 in list1:
        for item2 in list2:
            if item1 == item2:
                count += 1
                break
    return count / len(list1)


def compare_digit(str1, str2):
    list1 = list(jieba.cut(str1))
    list2 = list(jieba.cut(str2))

    digit1 = []
    digit2 = []
    for s in list1:
        if s.isdigit():
            digit1.append(s)

    for s in list2:
        if s.isdigit():
            digit2.append(s)
    if len(digit2) > len(digit1):
        temp = digit1
        digit1 = digit2
        digit2 = temp
    for s in digit2:
        if s not in digit1:
            return False
    return True


def get_value(value: dict, v_path: str):
    segs = v_path.split('.')
    v = value
    try:
        for seg in segs:
            v = v.get(seg)
            if v is None:
                return None
        return v
    except:
        return None


def process_located(located: str):
    located = located[:]
    if located[-1] == '号':
        return located[:-1] + '室'
    fix = ['-', '#']
    for f in fix:
        if f in located:
            inx = located.index('-')
            # 如果前一个也为数字的数字的话可能为8-101
            if located[inx - 1].isdigit():
                return located[0:inx] + '幢' + located[inx + 1:] + '室'
    return located
